﻿namespace TestTool.GUI.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using TestTool.GUI.Controllers;
    using TestTool.GUI.Enums;
    using TestTool.GUI.Views;

    internal abstract class Page : UserControl, IView
    {
        protected Page()
        {
        }

        protected void DisableControls(IEnumerable<Control> controls)
        {
            foreach (Control control in controls)
            {
                control.Enabled = false;
            }
        }

        protected void EnableControls(IEnumerable<Control> controls)
        {
            foreach (Control control in controls)
            {
                control.Enabled = true;
            }
        }

        public abstract IController GetController();
        public virtual void ShowError(Exception e)
        {
            string str = (e.Message.Length > 400) ? e.Message.Substring(0, 400) : e.Message;
            string message = "Unexpected error occurred: " + str;
            this.ShowErrorMessageBox(message);
        }

        public virtual void ShowError(string message)
        {
            this.ShowErrorMessageBox(message);
        }

        private void ShowErrorMessageBox(string message)
        {
            Action method = null;
            if (base.InvokeRequired)
            {
                if (method == null)
                {
                    method = () => MessageBox.Show(this, message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                base.BeginInvoke(method);
            }
            else
            {
                MessageBox.Show(this, message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        protected void ShowPrompt(string message, string caption)
        {
            MessageBox.Show(this, message, caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        public abstract void SwitchToState(TestTool.GUI.Enums.ApplicationState state);
    }
}

